home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Interface / AboutBox.c next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  6.9 KB  |  331 lines

  1. /*****
  2.  *
  3.  *    AboutBox.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompileWithForeground
  18.  
  19.  
  20. /* InternetConfig is not supported with Symantec PPC compiler, yet */
  21. #if __SC__
  22.     #ifdef kCompileWithAboutURL
  23.         #undef kCompileWithAboutURL
  24.         #define kCompileWithAboutURL    0
  25.     #endif
  26. #endif
  27.  
  28. #if kCompileWithAboutURL
  29. #include <ICAPI.h>
  30. #endif
  31.  
  32. #include "constants.h"
  33. #include "globals.h"
  34.  
  35. #include "DebugUtil.h"
  36. #include "IconUtil.h"
  37. #include "StringUtil.h"
  38. #include "WindowInt.h"
  39.  
  40. #include "AboutBox.h"
  41.  
  42.  
  43. /***  LOCAL VARIABLES ***/
  44.  
  45. static    DialogPtr        vAboutBoxDialog;
  46. static    Handle            vAboutIconSuiteHdl;
  47. static    UserItemUPP        vAboutIconUPP;
  48. #if kCompileWithAboutURL
  49. static    UserItemUPP        vAboutURLUPP;
  50. static    Str255            vAboutBoxURL;
  51. #endif
  52.  
  53. /***  LOCAL CONSTANTS ***/
  54.  
  55. #define kriAboutBoxAppIcon        2
  56. #define kriAboutBoxURL            3
  57.  
  58.  
  59. /***  LOCAL PROTOTYPES ***/
  60.  
  61. pascal void        aboutBoxAppIcon    ( DialogPtr, short );
  62. #if kCompileWithAboutURL
  63. pascal void        aboutBoxURL        ( DialogPtr, short );
  64.     ICError        URLDownload        ( StringPtr );
  65. #endif
  66.  
  67.  
  68. /***  FUNCTIONS  ***/
  69.  
  70. #pragma segment Startup
  71. /* initialize the variables used for the about box */
  72. void
  73. AboutBoxInit ( void )
  74. {
  75.     vAboutBoxDialog        = NULL;
  76.     vAboutIconSuiteHdl    = NULL;
  77.     vAboutIconUPP        = NULL;
  78.     
  79.     #if kCompileWithAboutURL
  80.     vAboutURLUPP = NULL;
  81.     GetIndString ( vAboutBoxURL, krURLStrings, 1 );
  82.     #endif
  83. } /* AboutBoxInit */
  84. #pragma segment Utilities
  85.  
  86.  
  87. /* open the about box dialog window */ 
  88. void
  89. AboutBoxOpen ( void )
  90. {
  91.     short            itemType;
  92.     Handle            item;
  93.     Rect            box;
  94.     GrafPtr            savePort;
  95.     
  96.     if ( vAboutBoxDialog == NULL )
  97.     {
  98.         vAboutBoxDialog = GetNewDialog ( kAboutBoxDLOG, NULL, (WindowPtr)-1L );
  99.         
  100.         if ( vAboutBoxDialog != nil )
  101.         {
  102.             WindowNewSetInfo ( vAboutBoxDialog, Window_about, false, nil );
  103.             
  104.             if ( vAboutIconUPP == NULL )
  105.             {
  106.                 vAboutIconUPP = NewUserItemProc ( aboutBoxAppIcon );
  107.             }
  108.             /* install the drawing procedure for the application icon user item */
  109.             GetDialogItem ( vAboutBoxDialog, kriAboutBoxAppIcon, &itemType, &item, &box );
  110.             SetDialogItem ( vAboutBoxDialog, kriAboutBoxAppIcon, itemType, (Handle)vAboutIconUPP, &box );
  111.             
  112.             #if kCompileWithAboutURL
  113.             if ( vAboutURLUPP == NULL )
  114.             {
  115.                 vAboutURLUPP = NewUserItemProc ( aboutBoxURL );
  116.             }
  117.             /* install the drawing procedure for the application icon user item */
  118.             GetDialogItem ( vAboutBoxDialog, kriAboutBoxURL, &itemType, &item, &box );
  119.             SetDialogItem ( vAboutBoxDialog, kriAboutBoxURL, itemType, (Handle)vAboutURLUPP, &box );
  120.             #endif /* kCompileWithAboutURL */
  121.         }
  122.     
  123.         if ( vAboutBoxDialog != NULL )
  124.         {
  125.             /* set the ^0 parameter to be the version string */
  126.             ParamText ( gVersionStr, kTargetPString, NULL, NULL );
  127.             
  128.             /* change the dialog port font to Geneva */
  129.             GetPort        ( &savePort );
  130.             SetPort        ( (GrafPtr)vAboutBoxDialog );
  131.             TextFont    ( geneva );
  132.             SetPort        ( savePort );
  133.         }
  134.     }
  135.     
  136.     /* display the dialog window and bring it to front */
  137.     if ( vAboutBoxDialog != NULL )
  138.     {
  139.         ShowWindow        ( vAboutBoxDialog );
  140.         SelectWindow    ( vAboutBoxDialog );
  141.     }
  142. } /* AboutBoxOpen */
  143.  
  144.  
  145. /* update the contents of the about box dialog window */
  146. void
  147. AboutBoxUpdate ( void )
  148. {
  149.     my_assert ( vAboutBoxDialog != NULL, "\pAboutBoxUpdate: vAboutBoxDialog is NULL" );
  150.     
  151.     /* set the dialog strings */
  152.     ParamText ( gVersionStr, kTargetPString, NULL, NULL );
  153.     
  154.     //• may need to save the port and set the font here
  155.     
  156.     /* draw the dialog */
  157.     DrawDialog ( vAboutBoxDialog );
  158. } /* AboutBoxUpdate */
  159.  
  160.  
  161. /* close the about box window */
  162. void
  163. AboutBoxClose ( void )
  164. {
  165.     windowInfoHdl    dlogWindInfo;
  166.     
  167.     my_assert ( vAboutBoxDialog != NULL, "\pAboutBoxClose: vAboutBoxDialog is NULL" );
  168.     
  169.     dlogWindInfo = (windowInfoHdl) GetWRefCon ( (WindowRef)vAboutBoxDialog );
  170.     
  171.     if ( dlogWindInfo != NULL )
  172.     {
  173.         DisposeHandle ( (Handle)dlogWindInfo );
  174.     }
  175.     
  176.     DisposeDialog ( vAboutBoxDialog );
  177.     
  178.     vAboutBoxDialog = NULL;
  179. } /* AboutBoxClose */
  180.  
  181.  
  182. /* Handle events in the about box */
  183. Boolean
  184. AboutHandleEvent ( EventRecord *theEvent, DialogPtr theDialog, short itemHit )
  185. {
  186.     if ( theDialog == vAboutBoxDialog )
  187.     {
  188.         /* if it's the about box dialog, handle the event */
  189.         switch ( theEvent->what ) 
  190.         {
  191.             case mouseDown:    
  192.                 switch ( itemHit )
  193.                 {
  194.                     #if kCompileWithAboutURL
  195.                     case kriAboutBoxURL :
  196.                         URLDownload ( vAboutBoxURL );
  197.                         break;
  198.                     #endif
  199.                 }
  200.                 break;
  201.             
  202.             case mouseUp:
  203.                 /* don't do anything */
  204.                 break;
  205.         }
  206.         
  207.         return true;
  208.     }
  209.     else
  210.     {
  211.         return false;
  212.     }
  213. } /*  */
  214.  
  215.  
  216. /* Draw the application icon using the ICN# or a color icon resource */
  217. pascal void
  218. aboutBoxAppIcon ( DialogPtr theDialog, short theItem )
  219. {
  220.     short        itemType;
  221.     Rect        itemRect;
  222.     Handle        itemHandle;
  223.     PenState    curPen;
  224.     WindowPtr    oldPort;
  225.     
  226.     GetDItem ( theDialog, theItem, &itemType, &itemHandle, &itemRect );
  227.     
  228.     GetPort ( &oldPort );
  229.     SetPort ( (GrafPtr)theDialog );
  230.     
  231.     GetPenState    ( &curPen );
  232.     PenNormal    ();
  233.     
  234.     IconDrawFromFamily ( kApplicationIconSuite, &itemRect, &vAboutIconSuiteHdl );
  235.  
  236.     SetPenState    ( &curPen );
  237.     SetPort        ( oldPort );
  238. } /* aboutBoxAppIcon */
  239.  
  240.  
  241. #pragma mark -
  242. #if kCompileWithAboutURL
  243.  
  244. /* Draw the URL string */
  245. pascal void
  246. aboutBoxURL ( DialogPtr theDialog, short theItem )
  247. {
  248.     short        itemType;
  249.     Rect        itemRect;
  250.     Handle        itemHandle;
  251.     PenState    curPen;
  252.     WindowPtr    oldPort;
  253.     short        oldFont;
  254.     short        oldSize;
  255.     short        oldFace;
  256.     RGBColor    oldColor;
  257.     RGBColor    blueColor;
  258.     
  259.     GetDItem ( theDialog, theItem, &itemType, &itemHandle, &itemRect );
  260.     
  261.     GetPort ( &oldPort );
  262.     SetGrafPortOfDialog ( theDialog );
  263.     oldFont = oldPort->txFont;
  264.     oldSize = oldPort->txSize;
  265.     oldFace = oldPort->txFace;
  266.     GetForeColor ( &oldColor );
  267.     
  268.     GetPenState    ( &curPen );
  269.     PenNormal    ();
  270.     
  271.     /* set the text format */
  272.     TextFont ( geneva );
  273.     TextSize ( 9 );
  274.     TextFace ( underline );
  275.     
  276.     blueColor.red = nil;
  277.     blueColor.green = nil;
  278.     blueColor.blue = 0xFFFF;
  279.     RGBForeColor ( &blueColor );
  280.     
  281.     /* draw the string */
  282.     StringDrawInRect ( vAboutBoxURL, itemRect );
  283.     
  284.     RGBForeColor ( &oldColor );
  285.     SetPenState    ( &curPen );
  286.     TextFace    ( oldFace );
  287.     TextSize    ( oldSize );
  288.     TextFont    ( oldFont );
  289.     SetPort        ( oldPort );
  290. } /* aboutBoxURL */
  291.  
  292.  
  293. /* Note that this routine can only succeed if Internet Config 1.1 (or better) is
  294.     installed on this Mac. Version 1.0 does not support the ICLaunchURL call. */
  295. ICError
  296. URLDownload ( StringPtr theURL )
  297. {
  298.     ICError        theErr;
  299.     ICInstance    ICInst;
  300.     long        SelStart;
  301.     long        SelEnd;
  302.     
  303.     theErr = ICStart ( &ICInst, kMyCreatorType );
  304.     if ( theErr == noErr )
  305.     {
  306.         theErr = ICFindConfigFile ( ICInst, nil, NULL );
  307.         if ( theErr == noErr )
  308.         {
  309.             SelStart = nil;
  310.             SelEnd   = *theURL;
  311.             
  312.             theErr = ICLaunchURL ( ICInst, "\p", (Ptr)(theURL + 1), *theURL, &SelStart, &SelEnd );
  313.         }
  314.         
  315.         ICStop ( ICInst );
  316.     }
  317.     
  318.     if ( theErr != noErr )
  319.     {
  320.         SysBeep ( 1 );
  321.     }
  322.     
  323.     return theErr;
  324. } /* URLDownload */
  325.  
  326. #endif /* kCompileWithAboutURL */
  327.  
  328. #endif    /* kCompileWithForeground */
  329.  
  330. /*****  EOF  *****/
  331.